package com.slmxz.tms.biz.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.slmxz.tms.biz.enums.OrderSpecialTypeEnum;
import com.slmxz.tms.biz.enums.OrderStatusEnum;
import com.slmxz.tms.biz.enums.TransportTypeEnum;
import com.slmxz.tms.biz.mapper.TransitMapper;
import com.slmxz.tms.biz.model.entity.Order;
import com.slmxz.tms.biz.model.vo.OrderVO;
import com.slmxz.tms.biz.model.vo.TransitVO;
import com.slmxz.tms.biz.service.TransitService;
import com.slmxz.tms.common.base.IBaseEnum;
import com.slmxz.tms.common.component.RedisUserService;
import com.slmxz.tms.common.constant.SystemConstants;
import com.slmxz.tms.core.service.DataScopeService;
import com.slmxz.tms.system.model.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：albert
 * @Date：2025/7/20 14:14
 */
@Service
@RequiredArgsConstructor
public class TransitServiceImpl  implements TransitService, DataScopeService<Order> {

    private final TransitMapper transitMapper;

    @Override
    public IPage<TransitVO> selectTransitPage(IPage<TransitVO> page, TransitVO transit) {
        UserInfoVO user = RedisUserService.currentUser();
        try{
            this.dataScopeHandler(transit);
        }catch (Exception e){
            transit.setScopeNo(user.getScopeNo());
            transit.setDataOperate(SystemConstants.OPERATE_EQUALS);
        }
        transit.setCompanyId(user.getCompanyId());

        List<TransitVO> results = this.transitMapper.selectTransitPage(page, transit);
        if(CollectionUtil.isNotEmpty(results)){
            results.forEach(orderItem -> {
                orderItem.setOrderStatusLabel(IBaseEnum.getLabelByValue(orderItem.getOrderStatus(), OrderStatusEnum.class));
                orderItem.setSpecialTypeLabel(IBaseEnum.getLabelByValue(orderItem.getSpecialType(), OrderSpecialTypeEnum.class));
                orderItem.setTransportTypeLabel(IBaseEnum.getLabelByValue(orderItem.getTransportType(), TransportTypeEnum.class));
            });
        }
        return page.setRecords(results);
    }
}
